﻿using ProsonUser.IProsonUser;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using ProsonUser.Users;
using System.Threading.Tasks;
using ProsonUser.ProsonUserDTO;
using Volo.Abp.ObjectMapping;
using ecd_csharp_demo;
using BWShop.Common;
using Microsoft.AspNetCore.Http;

namespace ProsonUser.ProsonUser
{
    public  class PUser: ApplicationService, IUser
    {
        private readonly IRepository<User, int> _users;
        public PUser(IRepository<User, int> users) 
        {
            _users = users;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public  UserDTO GetUser(string userEmail)
        {
            var query =  _users.Where(s=>s.UserEmail.Equals(userEmail)||s.UserPhone.Equals(userEmail)).FirstOrDefault();
            return ObjectMapper.Map<User, UserDTO>(query);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> ListUser(string userEmail,string userPwd)
        {
            var query = _users.Where(s => s.UserEmail.Equals(userEmail) && s.UserPwd.Equals(userPwd)).Count();
            if (query > 0)
            {
                return 1;
            }
            else
            {
                var query1 = _users.Where(s => s.UserPhone.Equals(userEmail) && s.UserPwd.Equals(userPwd)).Count();
                if (query1 > 0)
                {
                    return 2;
                }
                else
                {
                    return 0;
                }
            }
            
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="userDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> Roll(UserCreateDTO userDTO)
        {
            
            //短信
            string ret = CloudInfDemo.sendSmsCode(userDTO.UserPhone, 1, "206773");
          
            
            userDTO.UserCreateTime=DateTime.Now;
            //雪花Id
            IdWorker idworker = new IdWorker(1);
            userDTO.UserSnowflakeID =idworker.nextId().ToString();
            var lst = await _users.InsertAsync(ObjectMapper.Map<UserCreateDTO, User>(userDTO));
            if (ret != null && lst != null)
            {
                return 10;

            }
            else
            {

                return 0;
            }
        }
        /// <summary>
        /// 获取用户Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public User Gets(int id)
        {
            var query = _users.Where(s => s.Id.Equals(id)).FirstOrDefault();
            return query;
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> UpdateUser(UserDTO dto)
        {
            User user = await _users.GetAsync(dto.Id);
            user.UserName = dto.UserName;
            user.UserPhone = dto.UserPhone;
            user.UserEmail = dto.UserEmail;
            user.UserSex = dto.UserSex;
            return 1;

        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="oldPsw"></param>
        /// <param name="newPsw"></param>
        /// <param name="reNewPsw"></param>
        /// <returns></returns>
        [HttpPost,Route("UpPwd")]
        public async Task<int> GetPwd(int id, string oldPsw,string newPsw,string reNewPsw) //oldPsw旧密码 ，newPsw新密码，reNewPsw：确认密码
        {
            //10：旧密码输入错误
            //20：两次密码不一致
            //30:全部成功

            User user = await _users.GetAsync(id);
            if (user.UserPwd != oldPsw)
            {
                return 10;//10：旧密码输入错误
            }
            
                if (reNewPsw != newPsw)
                {
                    return 20;//20：两次密码不一致
            }
                else
                {
                    user.UserPwd = newPsw;
                    return 30; //30:修改成功
            }
            

            
        }




        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">来自form表单的文件信息</param>
        /// <returns></returns>
        [HttpPost,Route("Upload")]
        public  string Upload(IFormFile file)
        {
            string rootdir = AppContext.BaseDirectory.Split(@"\bin\")[0];
            string fname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + System.IO.Path.GetExtension(file.FileName);
            var path = rootdir + @"\Upload\" + fname;
            using (System.IO.FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Flush();//清空文件流
            }
            return "http://localhost:8012/" + fname;//将能访问新文件的网址回传给前端
        }
        /// <summary>
        /// 修改图片
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, Route("UpImg")]
        public async Task<int> UpImg(UserCreateDTO dto)
        {
            User lst = _users.Where(s=>s.Id.Equals(dto.Id)).FirstOrDefault();
            lst = await _users.FindAsync(lst.Id);
            lst.UserImg = dto.UserImg;
            return 1;
           
        }


    }
}
